﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SeniorCapstoneProject.ServiceReference3;
using System.IO;

namespace SeniorCapstoneProject
{
    public partial class frmAdmin : Form
    {
        private int adminMethod = 0;
        private List<FileInformation> fim = new List<FileInformation>();
        private verifyUser uLogin = new verifyUser();
        private List<ServiceReference3.HelperClass> gTemp = new List<ServiceReference3.HelperClass>();
        private List<string> userTemp = new List<string>();

        public frmAdmin(int i)
        {
            InitializeComponent(i);
            adminMethod = i;
        }

        public frmAdmin(int i, List<FileInformation> fim, verifyUser uLogin)
        {
            InitializeComponent(i);
            adminMethod = i;
            this.fim = fim;
            this.uLogin = uLogin;
        }

        public frmAdmin(int i, verifyUser uLogin)
        {
            InitializeComponent(i);
            adminMethod = i;
            this.uLogin = uLogin;
        }

        private void frmAdmin_Load(object sender, EventArgs e)
        {
            TransferFilesClient tfc = new TransferFilesClient();
            gTemp = tfc.GetGroups().ToList<ServiceReference3.HelperClass>();

            switch (adminMethod)
            {
                case 1:
                    foreach (ServiceReference3.HelperClass trial in gTemp)
                    {
                        this.cmbGroup.Items.Add(trial.GroupName);
                    }
                    break;
                case 2:
                    foreach (ServiceReference3.HelperClass trial in gTemp)
                    {
                        this.cmbGroup.Items.Add(trial.GroupName);
                    }
                    foreach (ServiceReference3.HelperClass trial in gTemp)
                    {
                        List<ServiceReference3.HelperClass> users = tfc.GetUsers(uLogin.Username, uLogin.Password, trial.GroupName).ToList();
                        foreach (ServiceReference3.HelperClass uTemp in users)
                        {
                            userTemp.Add(uTemp.UserName.ToString());
                        }
                    }
                    break;
                case 3:
                    foreach (ServiceReference3.HelperClass trial in gTemp)
                    {
                        this.cmbGroup.Items.Add(trial.GroupName);
                    }
                    break;
                case 5:
                    foreach (ServiceReference3.HelperClass trial in gTemp)
                    {
                        this.cmbGroup.Items.Add(trial.GroupName);
                    }
                    break;
                case 6:
                    foreach (ServiceReference3.HelperClass trial in gTemp)
                    {
                        this.cmbGroup.Items.Add(trial.GroupName);
                    }
                    break;
            }
        }

        private void cmbGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            TransferFilesClient tfc = new TransferFilesClient();

            switch (adminMethod)
            {
                case 1:
                    cmbFile.Items.Clear();
                    foreach (FileInformation temp in fim)
                    {
                        if (temp.GroupName == cmbGroup.SelectedItem.ToString())
                        {
                            cmbFile.Items.Add(Path.GetFileName(temp.FileName));
                        }
                    }
                    break;
                case 3:
                    cmbUsername.Items.Clear();
                    List<ServiceReference3.HelperClass> users = tfc.GetUsers(uLogin.Username, uLogin.Password, cmbGroup.SelectedItem.ToString()).ToList();
                    foreach (ServiceReference3.HelperClass uTemp in users)
                    {
                        if (uTemp.UserName != uLogin.Username)
                        {
                            cmbUsername.Items.Add(uTemp.UserName);
                        }
                    }
                    break;
                case 6:
                    cmbUsername.Items.Clear();
                    users = tfc.GetUsers(uLogin.Username, uLogin.Password, cmbGroup.SelectedItem.ToString()).ToList();
                    foreach (ServiceReference3.HelperClass uTemp in users)
                    {
                        if (uTemp.UserName != uLogin.Username)
                        {
                            cmbUsername.Items.Add(uTemp.UserName);
                        }
                    }
                    break;
            }
        }

        private void frmAdmin_FormClosing(object sender, FormClosingEventArgs e)
        {
            frmMain mainMessage = new frmMain();
            TransferFilesClient tfc = new TransferFilesClient();
            string passTemp;

            if (this.DialogResult == DialogResult.OK)
            {
                switch (adminMethod)
                {
                    case 1:
                        //add method name to remove file from group.
                        if (uLogin.Password == uLogin.encryptPass(txtPassword.Text))
                        {
                            foreach (FileInformation temp in fim)
                            {
                                if (Path.GetFileName(temp.FileName) == cmbFile.SelectedItem.ToString() && temp.GroupName == cmbGroup.SelectedItem.ToString())
                                {
                                    tfc.DeleteFile(uLogin.Username, uLogin.Password, temp.FileName);
                                    break;
                                }
                            }
                        }
                        else
                        {
                            mainMessage.customMessage("Error!!!", "Administration password is incorrect.", "The admin password you entered is invalid. Please enter the correct password or cancel.", "OK", null);
                            e.Cancel = true;
                            break;
                        }
                        break;
                    case 2:
                        //add method name to add user to a group.
                        foreach (string uTemp in userTemp)
                        {
                            if (uTemp == txtUsername.Text)
                            {
                                mainMessage.customMessage("Error!!!", "Username already in use", "Please select a different username.", "OK", null);
                                e.Cancel = true;
                                break;
                            }
                        }
                        if (txtUserPassword.Text.Length < 6)
                        {
                            mainMessage.customMessage("Error!!!", "Password is to short.", "The password must be atleast 6 characters long.", "OK", null);
                            e.Cancel = true;
                            break;
                        }
                        passTemp = uLogin.encryptPass(txtPassword.Text);
                        if (passTemp != uLogin.Password)
                        {
                            mainMessage.customMessage("Error!!!", "Administration password is incorrect.", "The admin password you entered is invalid. Please enter the correct password or cancel.", "OK", null);
                            e.Cancel = true;
                            break;
                        }
                        ServiceReference3.HelperClass hcu = new ServiceReference3.HelperClass();
                        ServiceReference3.HelperClass hcnu = new ServiceReference3.HelperClass();
                        hcu.UserName = uLogin.Username;
                        hcu.PassHash = uLogin.Password;
                        hcnu.UserName = txtUsername.Text;
                        hcnu.PassHash = passTemp;
                        hcnu.UserGroup = cmbGroup.SelectedItem.ToString();
                        int i = tfc.AddUser(hcu, hcnu);
                        if (i == 99)
                        {
                            mainMessage.customMessage("Error!!!", "UHOH", "Something went wrong on the server side. Please contact support.", "OK", null);
                            e.Cancel = true;
                            break;
                        }
                        break;
                    case 3:
                        //add method name to delete user.
                        passTemp = uLogin.encryptPass(txtPassword.Text);
                        if (cmbGroup.SelectedItem != null && cmbUsername.SelectedItem != null && txtPassword.Text != uLogin.Password)
                        {
                            tfc = new TransferFilesClient();
                            i = tfc.DeleteUser(uLogin.Username, uLogin.Password, cmbUsername.SelectedItem.ToString());
                            if (i == 99)
                            {
                                mainMessage.customMessage("Error!!!", "UHOH", "Something went wrong on the server side. Please contact support.", "OK", null);
                                e.Cancel = true;
                                break;
                            }
                        }
                        else
                        {
                            mainMessage.customMessage("Error!!!", "Missing element", "You must select a group and a username from that group plus enter in the correct admin password.", "OK", null);
                            e.Cancel = true;
                            break;
                        }
                        break;
                    case 4:
                        foreach (ServiceReference3.HelperClass temp in gTemp)
                        {
                            if (temp.GroupName == txtGroup.Text)
                            {
                                mainMessage.customMessage("Error!!!", "Bad Group Name", "That group name is already in use. Please chose a different name.", "OK", null);
                                e.Cancel = true;
                                break;
                            }
                        }
                        passTemp = uLogin.encryptPass(txtPassword.Text);
                        if(passTemp != uLogin.Password)
                        {
                            mainMessage.customMessage("Error!!!", "Bad Password", "The password provided does not match.", "OK", null);
                            e.Cancel = true;
                            break;
                        }
                        i = tfc.AddGroup(uLogin.Username, uLogin.Password, txtGroup.Text.ToString());
                        if (i == 99)
                        {
                            mainMessage.customMessage("Error!!!", "UHOH", "Something went wrong on the server side. Please contact support.", "OK", null);
                            e.Cancel = true;
                            break;
                        }
                        break;
                    case 5:
                        if (cmbGroup.SelectedItem == null)
                        {
                            mainMessage.customMessage("Error!!!", "Select a group", "Please select a group from the list before continueing.", "OK", null);
                            e.Cancel = true;
                            break;
                        }
                        passTemp = uLogin.encryptPass(txtPassword.Text);
                        if(passTemp != uLogin.Password)
                        {
                            mainMessage.customMessage("Error!!!", "Bad Password", "The password provided does not match.", "OK", null);
                            e.Cancel = true;
                            break;
                        }
                        i = tfc.DelGroup(uLogin.Username, uLogin.Password, cmbGroup.SelectedItem.ToString());
                        if (i == 99)
                        {
                            mainMessage.customMessage("Error!!!", "UHOH", "Something went wrong on the server side. Please contact support.", "OK", null);
                            e.Cancel = true;
                            break;
                        }
                        break;
                    case 6:
                        //add method name to change users password.
                        if (txtUserPassword.Text.Length < 6)
                        {
                            mainMessage.customMessage("Error!!!", "Password is to short.", "The password must be atleast 6 characters long.", "OK", null);
                            e.Cancel = true;
                            break;
                        }
                        string passUserTemp = uLogin.encryptPass(txtUserPassword.Text);
                        if (cmbGroup.SelectedItem == null || cmbUsername.SelectedItem == null)
                        {
                            mainMessage.customMessage("Error!!!", "Select group or user.", "Please select a group or user from the drop down boxes.", "OK", null);
                            e.Cancel = true;
                            break;
                        }
                        passTemp = uLogin.encryptPass(txtPassword.Text);
                        if (passTemp != uLogin.Password)
                        {
                            mainMessage.customMessage("Error!!!", "Administration password is incorrect.", "The admin password you entered is invalid. Please enter the correct password or cancel.", "OK", null);
                            e.Cancel = true;
                            break;
                        }
                        
                        i = tfc.ChangePassword(uLogin.Username, uLogin.Password, cmbUsername.SelectedItem.ToString(), passUserTemp);
                        if (i == 99)
                        {
                            mainMessage.customMessage("Error!!!", "UHOH", "Something went wrong on the server side. Please contact support.", "OK", null);
                            e.Cancel = true;
                            break;
                        }
                        break;
                }
            }
        }
    }
}
